package com.yonyou.pmclouds.alter;


import com.alibaba.dubbo.config.annotation.Reference;
import com.yonyou.pmclouds.ApplicationConsts;
import com.yonyou.pmclouds.alter.entity.AlterListViewVO;
import com.yonyou.pmclouds.alter.entity.AlterVO;
import com.yonyou.pmclouds.alter.rmiitf.AlterMaintain;
import com.yonyou.pmclouds.alter.rmiitf.AlterQuery;
import com.yonyou.pmclouds.altercategory.entity.AlterCategoryVO;
import com.yonyou.pmclouds.altercategory.rmiitf.AlterCategoryQuery;
import com.yonyou.pmclouds.basecom.annotation.Insert;
import com.yonyou.pmclouds.basecom.entity.AdditionalPropertiesVOWrapper;
import com.yonyou.pmclouds.basecom.exception.BusinessException;
import com.yonyou.pmclouds.basecom.service.CommonPropertiesProcessor;
import com.yonyou.pmclouds.offline.aspect.Offline;
import com.yonyou.pmclouds.offline.entity.OffLineDataKeyConst;
import com.yonyou.pmclouds.outter.enterprise.EnterpriseInfoQuery;
import com.yonyou.pmclouds.outter.user.UserInfoQuery;
import com.yonyou.pmclouds.team.entity.TeamMemberVO;
import com.yonyou.pmclouds.team.rmiitf.TeamEnterpriseQuery;
import com.yonyou.pmclouds.team.rmiitf.TeamMemberQuery;
import com.yonyou.pmclouds.workflow.aspect.PMApprove;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 变更申请
 */
@RequestMapping("/mobile/alter")
@RestController
@Offline(dataFrom = OffLineDataKeyConst.DATA_ALTER_CATEGORY)
public class AlterController {

    @Reference(version = ApplicationConsts.APPLICATION_VERSION)
    private AlterMaintain maintain;
    @Reference(version = ApplicationConsts.APPLICATION_VERSION)
    private AlterQuery alterQuery;

    @Reference(version = ApplicationConsts.APPLICATION_VERSION)
    private TeamMemberQuery teamMemberQuery;
    @Reference(version = ApplicationConsts.APPLICATION_VERSION)
    private TeamEnterpriseQuery enterpriseQuery;
    @Reference(version = ApplicationConsts.APPLICATION_VERSION)
    private AlterCategoryQuery categoryQuery;

    @Autowired
    private UserInfoQuery userQuery;
    @Autowired
    private EnterpriseInfoQuery enterpriseInfoQuery;
    @Autowired
    private CommonPropertiesProcessor propertiesProcessor;


    private static final String[] USER_ID_FIELDS = {AlterVO.CREATOR};
    private static final String[] USER_NAME_FIELDS = {AlterVO.CREATOR_NAME};

    /**
     * 提交
     */
    @Insert
    @RequestMapping(value = "/add")
    @PMApprove
    public AlterVO add(@RequestBody AlterVO alterVO) throws BusinessException {
        if (alterVO == null) return null;
        return maintain.insert(alterVO);
    }

    /**
     * 驳回修改
     */
    @RequestMapping(value = "/update")
    @PMApprove(trigger = "commit")
    public AlterVO update(@RequestBody AlterVO alterVO) throws BusinessException {
        if (alterVO == null) return null;
        return maintain.update(alterVO);
    }

    /**
     * 获取变更签证列表
     */
    @RequestMapping(value = "/getList")
    @ResponseBody
    public AdditionalPropertiesVOWrapper[] getList(@RequestParam("pkProject") String pkProject) throws BusinessException {
        if (StringUtils.isEmpty(pkProject)) {
            return new AdditionalPropertiesVOWrapper[0];
        }
        AlterListViewVO[] result = alterQuery.getAlterListInfo(pkProject);
        userQuery.tranUserInfoField(result, USER_ID_FIELDS, USER_NAME_FIELDS);
        result = result == null ? new AlterListViewVO[0] : result;
        return propertiesProcessor.wrapAdditionalProps(result);
    }

    /**
     * 获取变更签证详情
     */
    @RequestMapping(value = "/getDetail")
    @ResponseBody
    public AlterVO getDetail(@RequestParam("pkAlter") String pkAlter) throws BusinessException {
        if (StringUtils.isEmpty(pkAlter)) {
            return null;
        }
        AlterVO result = alterQuery.getById(pkAlter);
        userQuery.tranUserInfoField(new AlterVO[]{result}, USER_ID_FIELDS, USER_NAME_FIELDS);
        //企业信息添加
        enterpriseInfoQuery.tranEntInfoField(new AlterVO[]{result}, new String[]{AlterVO.PK_ENTERPRISE}, new String[]{TeamMemberVO.ENTERPRISE_NAME});
        return result;
    }

    /**
     * 获取变更分类列表
     */
    @RequestMapping(value = "/getCategoryList")
    public AlterCategoryVO[] getCategoryList(@RequestParam(name = "pkTenant") String pkTenant) throws BusinessException {
        AlterCategoryVO[] result = categoryQuery.queryCategoryListInfo(pkTenant);
        userQuery.tranUserInfoField(result, USER_ID_FIELDS, USER_NAME_FIELDS);
        Map<String, List<AlterCategoryVO>> childMap = new HashMap<>();
        Map<String, String> codeMap = new HashMap<>();//主键编码映射
        for (AlterCategoryVO vo : result) {
            codeMap.put(vo.getPkAltercategory(), vo.getScode());
        }
        for (AlterCategoryVO vo : result) {
            String parent = vo.getPkParent();
            if (StringUtils.isEmpty(parent)) {
                parent = StringUtils.EMPTY;
            } else {
                //前端需要子节点中存放父节点的编码
                vo.setPcode(codeMap.get(parent));
            }
            List<AlterCategoryVO> children = childMap.get(parent);
            if (children == null) {
                children = new ArrayList<>();
                childMap.put(parent, children);
            }
            children.add(vo);
        }
        for (AlterCategoryVO vo : result) {
            List<AlterCategoryVO> children = childMap.get(vo.getPkAltercategory());
            if (children != null) {
                vo.setChildCategory(children.toArray(new AlterCategoryVO[children.size()]));
            }
        }
        List<AlterCategoryVO> res = childMap.get(StringUtils.EMPTY);
        if (res == null) {
            return new AlterCategoryVO[0];
        } else {
            return res.toArray(new AlterCategoryVO[res.size()]);
        }
    }

    @Offline
    public AlterCategoryVO[] setTranUserInfoAndBuildTree(Object object) {
        AlterCategoryVO[] result = (AlterCategoryVO[])object;
        userQuery.tranUserInfoField(result, USER_ID_FIELDS, USER_NAME_FIELDS);
        Map<String, List<AlterCategoryVO>> childMap = new HashMap<>();
        Map<String, String> codeMap = new HashMap<>();//主键编码映射
        for (AlterCategoryVO vo : result) {
            codeMap.put(vo.getPkAltercategory(), vo.getScode());
        }
        for (AlterCategoryVO vo : result) {
            String parent = vo.getPkParent();
            if (StringUtils.isEmpty(parent)) {
                parent = StringUtils.EMPTY;
            } else {
                //前端需要子节点中存放父节点的编码
                vo.setPcode(codeMap.get(parent));
            }
            List<AlterCategoryVO> children = childMap.get(parent);
            if (children == null) {
                children = new ArrayList<>();
                childMap.put(parent, children);
            }
            children.add(vo);
        }
        for (AlterCategoryVO vo : result) {
            List<AlterCategoryVO> children = childMap.get(vo.getPkAltercategory());
            if (children != null) {
                vo.setChildCategory(children.toArray(new AlterCategoryVO[children.size()]));
            }
        }
        List<AlterCategoryVO> res = childMap.get(StringUtils.EMPTY);
        if (res == null) {
            return new AlterCategoryVO[0];
        } else {
            return res.toArray(new AlterCategoryVO[res.size()]);
        }
    }
}

